﻿using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Web.Mvc;
using MyMovieCollection.DATA.Models;
using MyMovieCollection.Services.Catalog;
using MyMovieCollection.Util;

namespace MyMovieCollection.Controllers
{
    public class ProductController : Controller
    {
        private readonly ICatalogService _catalogService;

        public ProductController()
        {
            _catalogService=new CatalogService();
        }
        public ProductController(ICatalogService catalogService)
        {
            _catalogService = catalogService; 
        }
        public ActionResult Index()
        {
            //AmazonSearcher.SearchByTitle("King");


            GetMovieTypes(0);
            return View("AddMovie");
        }

        public ActionResult Search()
        {
            GetPointScales(0);
            GetMovieTypes(0);
            ViewData["Search_Selected_Name"] = "";
            ViewData["Search_Selected_Year"] = "";
            ViewData["Movies"] = new List<Movie>();
            return View();
        }


        private void GetPointScales(int id)
        {
            List<PointScale> PointScales;
            if (TempData["Point_Scale"] != null)
            {
                PointScales = (List<PointScale>) ((SelectList) TempData["Point_Scale"]).Items;
            }
            else
            {
                PointScales = PointScale.GetPointScales();
            }
            ViewData["Point_Scale"] = new SelectList(PointScales, "ScaleID", "ScaleName", id);
            TempData["Point_Scale"] = ViewData["Point_Scale"];
        }

        private void GetMovieTypes(int id)
        {
            List<MovieType> MovieTypes = _catalogService.GetAllMovieTypes();
            MovieTypes.Sort(MovieType.NameComparerAsc());
            MovieTypes.Insert(0, new MovieType(0, "Lütfen Seçiniz"));

            ViewData["Movie_Type"] = new SelectList(MovieTypes, "ID", "TypeName", id);
        }

        private void GetMovies(int id, IComparer<Movie> comparer)
        {
            List<Movie> movies = _catalogService.GetAllMovies();
            movies.Sort(comparer);
            List<Movie> correctedMovies = id == 0 ? movies : movies.FindAll(m => (m.Movie_Type.ID == id));
            ViewData["Movies"] = correctedMovies;
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult MovieList(int id)
        {
            GetMovieTypes(id);
            GetMovies(id, Movie.NameComparerAsc());
            return View();
        }
        [CacheFilter(Order=1,Duration=15)]
        public ActionResult ListAllMovies()
        {
            List<MovieType> MovieTypes = _catalogService.GetAllMovieTypes();
            MovieTypes.Sort(MovieType.NameComparerAsc());
            MovieTypes.Insert(0, new MovieType(0, "Lütfen Seçiniz"));
            List<Movie> movies = _catalogService.GetAllMovies();
            movies.Sort(Movie.NameComparerAsc());
         
            ViewData["Movies"] = movies;
            ViewData["Movie_Type"] = new SelectList(MovieTypes, "ID", "TypeName", 0);

            return View("MovieList");
        }

        public ActionResult AddMovie()
        {
            var movie = new Movie();
            BindingHelperExtensions.UpdateFrom(movie, Request.Form);
            var posterPath = Request.Files["Poster"];
            posterPath.SaveAs(Server.MapPath("~") + "Content\\Upload\\" + Path.GetFileName(posterPath.FileName));
            movie.Poster = Path.GetFileName(posterPath.FileName);
            _catalogService.AddMovie(movie);
            MovieList(0);
            return View("MovieList");
        }

        public ActionResult AddCategory()
        {
            var movieType = new MovieType();
            BindingHelperExtensions.UpdateFrom(movieType, Request.Form);
            _catalogService.AddMovieType(movieType);

            List<MovieType> MovieTypes = _catalogService.GetAllMovieTypes();
            MovieTypes.Sort(MovieType.NameComparerAsc());
            MovieTypes.Insert(0, new MovieType(0, "Lütfen Seçiniz"));

            ViewData["Movie_Type"] = new SelectList(MovieTypes, "ID", "TypeName", movieType.ID);
            TempData["Movie_Type"] = ViewData["Movie_Type"];
            return View("AddMovie");
        }

        public ActionResult SortByMovieName(int id)
        {
            if (TempData["Sort_Asc_Name"] == null)
            {
                TempData["Sort_Asc_Name"] = false;
            }
            else
            {
                TempData["Sort_Asc_Name"] = !bool.Parse(TempData["Sort_Asc_Name"].ToString());
            }
            GetMovieTypes(id);
            if (bool.Parse(TempData["Sort_Asc_Name"].ToString()))
            {
                GetMovies(id, Movie.NameComparerAsc());
            }
            else
            {
                GetMovies(id, Movie.NameComparerDesc());
            }
            return View("MovieList");
        }

        public ActionResult SortByPoint(int id)
        {
            if (TempData["Sort_Asc_Point"] == null)
            {
                TempData["Sort_Asc_Point"] = false;
            }
            else
            {
                TempData["Sort_Asc_Point"] = !bool.Parse(TempData["Sort_Asc_Point"].ToString());
            }
            GetMovieTypes(id);
            if (bool.Parse(TempData["Sort_Asc_Point"].ToString()))
            {
                GetMovies(id, Movie.PointComparerAsc());
            }
            else
            {
                GetMovies(id, Movie.PointComparerDesc());
            }
            return View("MovieList");
        }

        public ActionResult ShowBig(string fileName, string MovieName)
        {
            ViewData["BigPosterFilepath"] = "../../../Content/Upload/" + fileName;
            Image img = Image.FromFile(Server.MapPath("~") + "Content/Upload/" + fileName);
            ViewData["SecreenX"] = img.Width + 20;
            ViewData["SecreenY"] = img.Height + 25;
            ViewData["Title"] = MovieName;
            return View();
        }

        public ActionResult SearchMovie(string Name, string Year, int Point_Scale, int Movie_Type)
        {
            PointScale scale = null;
            MovieType movieType = null;
            if (Point_Scale != 0)
            {
                scale = PointScale.ConvertToPointScale(Point_Scale);
            }
            if (Movie_Type != 0)
            {
                movieType = _catalogService.GetMovieType(Movie_Type);
            }
            int intYear = Year == "" ? 0 : int.Parse(Year);
            IList<Movie> movieList = _catalogService.GetAllMoviesByCriterias(Name, intYear, scale, movieType);
            ViewData["Movies"] = movieList;
            ViewData["Search_Selected_Name"] = Name;
            ViewData["Search_Selected_Year"] = Year;
            GetMovieTypes(Movie_Type);
            GetPointScales(Point_Scale);
            return View("Search");
        }

        public ActionResult Delete(int ID)
        {
            _catalogService.DeleteMovie(ID);
            return RedirectToAction("ListAllMovies");
        }
    }
}